<?php
namespace Events\model;

 use Zend\Db\ResultSet\ResultSet;
 use Zend\Db\TableGateway\TableGateway;
 use Zend\Db\Sql\Select;
 use Zend\Paginator\Adapter\DbSelect;
 use Zend\Paginator\Paginator;
 
class EventTable
{
    protected $tableGateway;
    
    function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway=$tableGateway;
    }
    
    public function fetchAll($paginated=false)
    {
        if ($paginated) {
            // create a new Select object for the table album
            $select = new Select('event');
            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Event());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
                // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype
            );
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }
    
    public function getEvent($id)
    {
        $rowset = $this->tableGateway->select(array('ID' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }
    
    public function fetchPendingEvents($paginated=false)
    {
        if ($paginated) {
            // create a new Select object for the table album
            $select = new Select();
            $select->from('event')->where('EVENT_STATUS = 1');
            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Event());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
                // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype
            );
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select(array('EVENT_STATUS' => 1));
        return $resultSet;
    }
    
    public function getLatestPendingEvent()
    {       
        $rowset = $this->tableGateway->select(function (Select $select) {
            $select->order('DATE DESC');
        });
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row");
        }
        return $row;
    }
    
    public function fetchCompletedEvents($paginated=false)
    {
        if ($paginated) {
            $select = new Select();
            $select->from('event')->where('EVENT_STATUS = 2');
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Event());
            $paginatorAdapter = new DbSelect(
                $select,
                $this->tableGateway->getAdapter(),
                $resultSetPrototype
            );
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select(array('EVENT_STATUS' => 2));
        return $resultSet;
    }
    
    public function getTableHeaders(){
        $columns=$this->tableGateway->getColumns();
        $value=0;
        $option=array();
        foreach ($columns As $column):
            if($value>0 && $value<9 && ($value!=5 && $value!=7)){
                	$option [] = array(
                	    'value' => $column,
                	    'label' => $column
                	);
            }
        $value++;    
        endforeach;    
        return $option;
    }
    
    public function search($paginated,$category,$key,$status){
        $select = new Select('event');
        if($category=='CATEGORY'){
            if($key=='main'){
                $key=1;
            }
            else if($key=="district"){
                $key=2;
            }
            else if ($key=="regional"){
                $key=3;
            }
        }
        if($category=='EVENT_STATUS'){
            if($key=='pending'){
                $key=1;
            }
            else if($key=='completed'){
                $key=2;
            }
        }
        
        if($status==1){
            $select->where('EVENT_STATUS = 1');
        }else if($status==2){
            $select->where('EVENT_STATUS = 2');
        }
        $select->where->like($category, '%'.$key.'%');
        
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new Event());
        $paginatorAdapter = new DbSelect(
            $select,
            $this->tableGateway->getAdapter(),
            $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }
    
    public function saveEvent(Event $event,$type)
    {
        $data = array(
               'ID' => $event->ID,
               'TITLE' => $event->TITLE,
               'DATE'  => $event->DATE,
               'SUBJECT' => $event->SUBJECT,
               'CATEGORY' => $event->CATEGORY,
               'SEATS' => $event->SEATS,
               'PLACE' =>$event->PLACE,
               'DESCRIPTION'=>$event->DESCRIPTION,
               'PLACE' =>$event->PLACE,
               'EVENT_STATUS' =>$event->EVENT_STATUS,   
        );
        $id = $event->ID;
        if ($type == 0) {
            $this->tableGateway->insert($data);
        } else if ($type == 1) {
            if ($this->getEvent($id)) {
                $this->tableGateway->update($data, array('ID' => $id));
            } else {
                throw new \Exception('User ID does not exist');
            }
        }
    }
}

?>